package com.example.controller;

import com.example.entity.Menu;
import com.example.pageutils.MyPageHelper;
import com.example.pageutils.PageRequest;
import com.example.pageutils.PageResult;
import com.example.result.CommonResult;
import com.example.service.MenuService;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("Menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    @GetMapping("/menu")
    public CommonResult getMenu(@RequestParam String menuName, PageRequest pageRequest){
        MyPageHelper.startPage(pageRequest);
        CommonResult commonResult =menuService.getMenu(menuName);
        return commonResult;
    }

    /**
     * 查询树
     * @return
     */
    @GetMapping("getMenuTree")
    public CommonResult getMenuTree(){
        return CommonResult.ok(menuService.getMenuTree());
    }

    /**
     * 新增树节点
     * @return
     */
    @PostMapping("/addPMenuTree")
    public CommonResult addpMenuTree(@RequestBody Menu menu) {
        int i = menuService.addpMenuTree(menu);
        if(i>0){
            return CommonResult.ok();
        }else {
            return CommonResult.fail();
        }
    }

    /**修改
     * @param menu
     * @return
     */
    @PutMapping("/updateMenu")
    public CommonResult updateMenu(@RequestBody Menu menu){
        int i = menuService.updateMenu(menu);
        if(i>0){
            return CommonResult.ok();
        }else {
            return CommonResult.fail();
        }
    }
    @DeleteMapping("/deleteMenu")
    public CommonResult deleteMenu(Integer id){
        return CommonResult.ok(menuService.deleMenu(id));
    }
}
